﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace OnlineCellphoneStore
{
    public partial class Info : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["ClientID"] == null)
                {
                    AnonymousContext();
                }
                else
                {
                    UserContext();
                }
                int ProductID = Int32.Parse(Request.QueryString["ProductID"]);
                MakeInfoTable(ProductID);
            }
        }
        private void AnonymousContext()
        {
            HandleAnonymousLables();
        }
        private void UserContext()
        {
            HandleUserLables();
            AddPurchaseNavigator();
        }
        protected void LogOut_Click(object sender, EventArgs e)
        {
            string Url = "Main.aspx?LogOut=true";
            Response.Redirect(Url, false);
        }
        protected void BtnBackToMain_Click(object sender, EventArgs e)
        {
            string Url = "Main.aspx";
            Response.Redirect(Url, false);
        }
        private void MakeInfoTable(int ProductID)
        {
            try
            {
                // Phone Data
                DAL.DBAdapter.CellphoneDisplayDataTable dt = SQLAdapter.GetPhoneInfo(ProductID);
                InfoTitle.InnerHtml = dt[0].Model;
                Year.InnerHtml = dt[0].Year.ToString();
                Proccessor.InnerHtml = dt[0].Proccessor;
                MemorySize.InnerHtml = dt[0].MemorySize;
                Resolution.InnerHtml = dt[0].Resolution;
                Width.InnerHtml = dt[0].Width + " mm";
                Height.InnerHtml = dt[0].Height + " mm";
                Thickness.InnerHtml = dt[0].Thickness + " mm";
                Weight.InnerHtml = dt[0].Weight + " g";
                CameraPixel.InnerHtml = dt[0].CamPixel + " pixels";
                Price.InnerHtml = dt[0].Price + " NIS";
                Manufacturer.InnerHtml = dt[0].Manufacture;
                Model.InnerHtml = dt[0].Model;
                // Phone Img
                Image Img = new Image();
                Img.ImageUrl = dt[0].PicturePath;
                Img.Width = 350;
                Img.Height = 350;
                Img.CssClass = "img-thumbnail";
                PhonePicture.Controls.Add(Img);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);

            }
        }
        private void AddPurchaseNavigator()
        {
            HtmlGenericControl li = new HtmlGenericControl("li");
            HtmlGenericControl anchor = new HtmlGenericControl("a");
            HtmlGenericControl span = new HtmlGenericControl("span");
            HtmlGenericControl text = new HtmlGenericControl("span");

            string Url = "UserPages/PreviousPurchase.aspx";
            anchor.ID = "RecentPurchasesLink";
            anchor.Attributes.Add("href", Url);
            text.InnerText = " My Previous Purchase";
            span.Attributes.Add("class", "glyphicon glyphicon-th-large");

            anchor.Controls.Add(span);
            anchor.Controls.Add(text);
            li.Controls.Add(anchor);
            NavBar.Controls.Add(li);
        }
        private void HandleUserLables()
        {
            ShopingCartLink.Attributes.Add("href", "UserPages/ShopingCart.aspx");
            anonymousUserOptions.Attributes.CssStyle.Value = "display: none;";
            registerdUserOptions.Attributes.CssStyle.Value = "display: inline;";
            aHelloUser.InnerHtml += Session["UserName"].ToString();
            aLastEntered.InnerHtml += Session["LastVisit"].ToString();
        }
        private void HandleAnonymousLables()
        {
            ShopingCartLink.Attributes.Add("href", "Account/Login.aspx");
        }
        protected void AddToCartBtn_Click(object sender, EventArgs e)
        {
            string Url;
            if (Session["ClientID"] == null)
            {
                Url = "Account/Login.aspx";
            }
            else
            {
                Url = "UserPages/ShopingCart.aspx?ProductID=" + Int32.Parse(Request.QueryString["ProductID"]);
            }
            Response.Redirect(Url, false);
        }
    }
}